/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.persistence;

import com.axiom.utility.IdGenerator;
import com.axiom.vo.instructor_detail;
import com.axiom.vo.instuctor_course;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shuhail
 */
public class instructor_course_persist {
    
    Session sess = null;
    Transaction tx = null;
    
    public static instructor_course_persist getInstance() {
        
        return new instructor_course_persist();
    }
    
    
    public instructor_course_persist() {
        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        sess = sf.openSession();
        tx = sess.beginTransaction();
    }
    
    
    public boolean insertFresh(instuctor_course instcourse) {
        
        try {
            instcourse.setInst_id(instructor_detail_persist.getInstance().getTheLastPK());
            sess.save(instcourse);
            tx.commit();
            return true;
        }
        catch(Exception e) {
            
            return false;
        }
        
    }
    
    
    public List getAllInstructor() {
        
        Query query = sess.getNamedQuery("getAllInstructorCourseDetails");
        return query.list();
    }
    
    
    public String getTheLastPK() {
        
        List list = this.getAllInstructor();
        Iterator it = list.iterator();
        List<String> pkList = new ArrayList<String>();
        while(it.hasNext())  {
            
            instuctor_course detail = (instuctor_course)it.next();
            pkList.add(detail.getInst_id());
        }
        
        String lastPK=IdGenerator.getInstance().getTheLastPK(pkList);
        return lastPK;
    }
    
    public int deleteInstructorCourse(String inst_id) {
        
        Query query = sess.getNamedQuery("deleteInstructorCourse");
        query.setParameter("inst_id", inst_id);
        try {
            
            int delRows = query.executeUpdate();
            return delRows;
        }
        catch(Exception e) {
            return 0;
        }
    }
    
        public static void main(String[] args) {
            
            instuctor_course c = new instuctor_course();
            c.setCourse_id("CC");
            //c.setInst_id("I2");
           int a = new instructor_course_persist().deleteInstructorCourse("I11");
           
            System.out.println(a);
        }
    }
    
   
